package com.viking.util.database.query;

import lombok.extern.flogger.Flogger;
import lombok.extern.slf4j.Slf4j;
import org.mortbay.log.Log;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDateTime;

@Slf4j
public class SampleDataBase {

    private static ResultSet resultSet;
    private static int Max;

    public static int getMax()  {
        try {
            Class.forName ("com.mysql.cj.jdbc.Driver");
//        2.用户信息和url
            String url = "jdbc:mysql://8.130.32.3:3306/testing?useUnicode=true&characterEncoding=utf-8";
            String username="testing";
            String password="Yiee4M4sHAaNXAMf";
//        3.连接成功，数据库对象 Connection
            Connection connection = DriverManager.getConnection(url,username,password);
//        4.执行SQL对象Statement，执行SQL的对象

            Statement stmt = connection.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            resultSet= stmt.executeQuery("SELECT COUNT('*') FROM sampledata");
            while(resultSet.next())
            {
                Max=resultSet.getInt(1);
            }
            resultSet.close();
            connection.close();
        }
       catch (Exception e)
       {
           LocalDateTime dt = LocalDateTime.now();
           log.error(dt+"Failed to obtain sample quantity");
       }
        return Max;

    }
    public static String[][] SampleDataBase(int start,int limit) throws Exception
    {
        String[][] Data=new String[limit][26];
        try {
            Class.forName ("com.mysql.cj.jdbc.Driver");
//        2.用户信息和url
            String url = "jdbc:mysql://8.130.32.3:3306/testing?useUnicode=true&characterEncoding=utf-8";
            String username="testing";
            String password="Yiee4M4sHAaNXAMf";
//        3.连接成功，数据库对象 Connection
            Connection connection = DriverManager.getConnection(url,username,password);
//        4.执行SQL对象Statement，执行SQL的对象

            Statement stmt = connection.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            resultSet= stmt.executeQuery("SELECT\n" +
                    "\tsampledata.*\n" +
                    "FROM\n" +
                    "\tsampledata\n" +
                    "LIMIT "+start+","+limit);
            int i=0;
            while(resultSet.next())
            {
                Data[i][0] = resultSet.getString(1);
                Data[i][1] = resultSet.getString(2);
                Data[i][2] = resultSet.getString(3);
                Data[i][3] = resultSet.getString(4);
                Data[i][4] = resultSet.getString(5);
                Data[i][5] = resultSet.getString(6);
                Data[i][6] = resultSet.getString(7);
                Data[i][7] = resultSet.getString(8);
                Data[i][8] = resultSet.getString(9);
                Data[i][9] = resultSet.getString(10);
                Data[i][10] = resultSet.getString(11);
                Data[i][11] = resultSet.getString(12);
                Data[i][12] = resultSet.getString(13);
                Data[i][13] = resultSet.getString(14);
                Data[i][14] = resultSet.getString(15);
                Data[i][15] = resultSet.getString(16);
                Data[i][16] = resultSet.getString(17);
                Data[i][17] = resultSet.getString(18);
                Data[i][18] = resultSet.getString(19);
                Data[i][19] = resultSet.getString(20);
                Data[i][20] = resultSet.getString(21);
                Data[i][21] = resultSet.getString(22);
                Data[i][22] = resultSet.getString(23);
                Data[i][23] = resultSet.getString(24);
                Data[i][24] = resultSet.getString(25);
                Data[i][25] = resultSet.getString(26);

                i++;
            }
            for(i=0;i<limit;i++){
                System.out.print(  Data[i][0]+"        ");
                System.out.print(  Data[i][1]+"        ");
                System.out.print(  Data[i][2]+"        ");
                System.out.print(  Data[i][3]+"        ");
                System.out.print(  Data[i][4]+"        ");
                System.out.print(  Data[i][5]+"        ");
                System.out.print(  Data[i][6]+"        ");
                System.out.print(  Data[i][7]+"        ");
                System.out.print(  Data[i][8]+"        ");
                System.out.print(  Data[i][9]+"        ");
                System.out.print(  Data[i][10]+"        ");
                System.out.print(  Data[i][11]+"        ");
                System.out.print(  Data[i][12]+"        ");
                System.out.print(  Data[i][13]+"        ");
                System.out.print(  Data[i][14]+"        ");
                System.out.print(  Data[i][15]+"        ");
                System.out.print(  Data[i][16]+"        ");
                System.out.print(  Data[i][17]+"        ");
                System.out.print(  Data[i][18]+"        ");
                System.out.print(  Data[i][19]+"        ");
                System.out.print(  Data[i][20]+"        ");
                System.out.print(  Data[i][21]+"        ");
                System.out.print(  Data[i][22]+"        ");
                System.out.print(  Data[i][23]+"        ");
                System.out.print(  Data[i][24]+"        ");
                System.out.print(  Data[i][25]+"        ");
                System.out.println("");
            }
        }
       catch (Exception e)
       {
           LocalDateTime dt = LocalDateTime.now();
           log.error(dt+"Failed to get sample database");
       }
        return Data;
    }
}
